package com.vertx.test.controller;

import com.vertx.test.vo.TestResponse;
import com.vertx.test.vertx.handler.VertxAbstractHandler;
import com.vertx.test.vo.User;
import io.vertx.core.Handler;
import io.vertx.ext.web.RoutingContext;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

@Component
@Slf4j
public class VertxController extends VertxAbstractHandler {

  /**
   * 添加逻辑处理方法，对应spring boot中的 controller
   */
  @Override
  public void registerRoute() {
    vertxService.get("/pet/v1/send/:name", testGetHandler);
    vertxService.post("/pet/v1/loading", testPostHandler);
  }

  /**
   * test get
   */
  private final Handler<RoutingContext> testGetHandler = ctx -> {
    String name = ctx.pathParam("name");
    log.info("path var:{}", name);
    User user = new User();
    user.setName("Mc.Download's");
    response(ctx, TestResponse.builder().user(user).build());
  };

  /**
   * test post
   */
  private final Handler<RoutingContext> testPostHandler = ctx -> {
    User user = requestBody(ctx, User.class);
    //log.info("user:{}", user.toString());
    response(ctx, TestResponse.builder().user(user).build());
  };

}
